package com.zzty.servlet;

import com.zzty.common.Log;
import com.zzty.dao.QuestionDao;
import com.zzty.daoimpl.QuestionDaoImpl;
import com.zzty.entity.Question;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name="ModelServlet",urlPatterns="/ModelServlet")
public class ModelServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(Log.class);
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if("insertTo".equals(method)){
            try {
                insertTo(request,response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if ("getTable".equals(method)) {
            getTable(request, response);
        }
    }
    protected void getTable(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        QuestionDao cd = new QuestionDaoImpl();
        List<Question> questions=null;
        String json=null;
        ObjectMapper mapper = new ObjectMapper();
        if(cd.getQuestionsToTable()!=null){
            questions=cd.getQuestionsToTable();
            json = mapper.writeValueAsString(questions);
        }
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void insertTo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String json1=readJSONData(request);
        ObjectMapper mapper = new ObjectMapper();

        Question question_new=mapper.readValue(json1,Question.class);
        String content = question_new.getContent();
        QuestionDao qd = new QuestionDaoImpl();
        int id = qd.insertContent(content);
        Question question = new Question();
        question.setId(id);
        question.setContent(content);

        String json = mapper.writeValueAsString(question);
        logger.info("json="+json);
        PrintWriter out = response.getWriter();
        out.write(json);
        out.flush();
        out.close();


    }
    private String readJSONData(HttpServletRequest request) {
        StringBuffer json=new StringBuffer();
        String lineString=null;
        try {
            BufferedReader reader=request.getReader();
            while ((lineString=reader.readLine())!=null) {
                json.append(lineString);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return json.toString();
    }

}
